package com.itcast.spring6.ioc.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 注册外部单体Bean, 放入spring进行过托管
 * @author zhangtian105
 * @date 2023/4/27
 * @apiNote
 */
@Slf4j
public class ThridOutBeanTest {
    public static void main(String[] args) {
        // 创建容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext() ;
        applicationContext.register(ThridOutBeanTest.class);

        // 外部Bean
        InitialzationBean initialzationBean = new InitialzationBean() ;
        // ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        SingletonBeanRegistry beanFactory = applicationContext.getBeanFactory();
        // 注册外部单例对象
        beanFactory.registerSingleton("initialzationBean", initialzationBean);

        // 启动应用上下文
        applicationContext.refresh();

        // 通过依赖查找的方式来获取InitialzationBean
        // InitialzationBean bean = beanFactory.getBean("initialzationBean", InitialzationBean.class) ;
        InitialzationBean bean = applicationContext.getBean("initialzationBean", InitialzationBean.class) ;
        log.info("initialzationBean == bean >>>: {}", (initialzationBean == bean)) ;
        // 关闭spring应用上下文
        applicationContext.close();
    }
}
